package git.soulbgm.mapreduce.utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @author SoulBGM
 * @date 2021/11/20 21:04
 */
public class DriverUtil {

    public static void createDriver(Class<?> jarByClass, Class<? extends Mapper> mapperClass, Class<? extends Reducer> reducerClass, Class<? extends Writable> mapOutputKeyClass, Class<? extends Writable> mapOutputValueClass, Class<? extends Writable> outputKeyClass, Class<? extends Writable> outputValueClass, Path inputPaths, Path outputPath) throws IOException, InterruptedException, ClassNotFoundException {
        Job job = createJob(jarByClass, mapperClass, reducerClass, mapOutputKeyClass, mapOutputValueClass, outputKeyClass, outputValueClass, inputPaths, outputPath);

        // 7 提交job
        boolean result = job.waitForCompletion(true);
        System.exit(result ? 0 : 1);
    }

    public static Job createJob(Class<?> jarByClass, Class<? extends Mapper> mapperClass, Class<? extends Reducer> reducerClass, Class<? extends Writable> mapOutputKeyClass, Class<? extends Writable> mapOutputValueClass, Class<? extends Writable> outputKeyClass, Class<? extends Writable> outputValueClass, Path inputPaths, Path outputPath) throws IOException {
        return createJob(new Configuration(), jarByClass, mapperClass, reducerClass, mapOutputKeyClass, mapOutputValueClass, outputKeyClass, outputValueClass, new Path[]{inputPaths}, outputPath);
    }

    public static Job createJob(Configuration conf, Class<?> jarByClass, Class<? extends Mapper> mapperClass, Class<? extends Reducer> reducerClass, Class<? extends Writable> mapOutputKeyClass, Class<? extends Writable> mapOutputValueClass, Class<? extends Writable> outputKeyClass, Class<? extends Writable> outputValueClass, Path[] inputPaths, Path outputPath) throws IOException {
        // 1 获取配置信息以及获取job对象
        Job job = Job.getInstance(conf);

        // 2 关联本Driver程序的jar
        job.setJarByClass(jarByClass);

        // 3 关联Mapper和Reducer的jar
        job.setMapperClass(mapperClass);
        if (reducerClass != null) {
            job.setReducerClass(reducerClass);
        }

        // 4 设置Mapper输出的kv类型
        job.setMapOutputKeyClass(mapOutputKeyClass);
        job.setMapOutputValueClass(mapOutputValueClass);

        // 5 设置最终输出kv类型
        job.setOutputKeyClass(outputKeyClass);
        job.setOutputValueClass(outputValueClass);

        // 6 设置输入和输出路径
        FileInputFormat.setInputPaths(job, inputPaths);
        FileOutputFormat.setOutputPath(job, outputPath);
        return job;
    }

}
